Ext.define('Emp.controller.DictController', {
    extend: 'Emp.controller.BaseController',

    models: ['Dict', 'Dictionary'],
    views: ['dict.List', 'dict.Edit'],
    refs: [
        {ref: 'dictList', selector: 'dictlist'},
        {ref: 'dictEdit', selector: 'dictedit', xtype: 'dictedit', autoCreate: true}
    ],
    
    init: function() {
        this.control({
        	'dictlist grid': {
        		'itemdblclick': this.doModifyOnList
        	},
            'dictlist gridtoolbar button[itemId=create]': {
                'click': this.doCreateOnList
            },
            'dictlist gridtoolbar button[itemId=modify]': {
                'click': this.doModifyOnList
            },
            'dictlist gridtoolbar button[itemId=read]': {
                'click': this.doReadOnList
            },
            'dictlist gridtoolbar button[itemId=remove]': {
                'click': this.doRemoveOnList
            },
            'dictedit': {
                'afterrender': this.doLoadOnEdit
            },
            'dictedit button[action=save]': {
                'click': this.doSaveOnEdit
            },
            'dictedit menuitem[action=save_close]': {
                'click': this.doSaveCloseOnEdit
            },
            'dictedit button[action=create]': {
                'click': this.doCreateOnEdit
            },
            'dictedit button[action=close]': {
                'click': this.doCloseOnEdit
            },
            'dictedit grid toolbar button[itemId=add_row]': {
                'click': this.doAddRow
            },
            'dictedit grid toolbar button[itemId=delete_row]': {
                'click': this.doDeleteRow
            }
        });
    },
    
    doAddRow: function() {
    	var edit = this.getDictEdit();
    	var store = edit.down('grid').getStore();
    	edit.gridEditing.cancelEdit();
    	var index = store.getCount();
        var r = Ext.create('Emp.model.Dictionary', {
        	dictId: edit.down('form').down('textfield[name=id]').getValue(),
        	status: 'enable',
        	sort: index + 1
        });
        store.insert(index, r);
        edit.gridEditing.startEdit(index, 2);
    },
    
    doDeleteRow: function() {
    	var edit = this.getDictEdit();
    	edit.gridEditing.cancelEdit();
		var grid = edit.down('grid');
		var	records = grid.getSelectionModel().getSelection();
		grid.getStore().remove(records);
    },
    
    //@override
    setValues: function(form, action) {
    	form.setValues(action.result.data);
		var store = this.getDictEdit().down('grid').getStore();
		store.removeAll();
		store.insert(0, action.result.datas);
		store.commitChanges();
    },
    
    //@override
    isValid: function() {
    	var edit = this.getDictEdit();
    	var formValid = edit.down('form').getForm().isValid();
    	var gridValid = true;
    	var store = edit.down('grid').getStore();
		for(var i=0; i<store.getCount(); i++) {
			if(!store.getAt(i).isValid()) {
				edit.gridEditing.startEdit(i, 2);
				gridValid = false;
				break;
			}
		}
		return formValid && gridValid;
    },
    
    //@override
    getSubmitParams: function() {
    	var me = this;
    	var store = me.getDictEdit().down('grid').getStore();
		var creates = new Array();
		var removes = new Array();
		var modifies = new Array();
		Ext.Array.each(store.getNewRecords(), function(record) {
	        creates.push(record.getData());
	    });
		Ext.Array.each(store.getRemovedRecords(), function(record) {
	        removes.push(record.get(record.idProperty));
	    });
		Ext.Array.each(store.getUpdatedRecords(), function(record) {
	        modifies.push(record.getData());
	    });
	    var params = {};
	    if(creates.length > 0) {
	    	params.creates = Ext.encode(creates);
	    }
	    if(removes.length > 0) {
	    	params.removes = Ext.encode(removes);
	    }
	    if(modifies.length > 0) {
	    	params.modifies = Ext.encode(modifies);
	    }
	    return params;
    },
    
    //override
    doCreateOnEdit: function() {
    	this.callParent();
    	var store = this.getDictEdit().down('grid').getStore();
		store.removeAll();
		store.commitChanges();
    },
    
    //override
    doCreateCopyOnEdit: function() {
    	this.callParent();
    	var store = this.getDictEdit().down('grid').getStore();
    	var records = new Array();
		var length = store.getCount();
		for(var i=length-1; i>= 0; i--) {
			var record = store.getAt(i);
			var r = Ext.create('Emp.model.Dictionary', {
				display: record.get('display'),
				value: record.get('value'),
	        	status: record.get('status'),
	        	sort: record.get('sort')
	        });
			records[i] = r;
			store.removeAt(i);
		}
		store.commitChanges();
		store.add(records);
    }
 });